package com.dunqi.gpm.cangxi.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.constance.Constance;

import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

public class ProjectTrendAutidActivity extends BaseAct {


    private static final String PRO_WORK_TREND_ID = "pro_work_trend_id";
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.pass_rb)
    RadioButton passRb;
    @BindView(R.id.not_pass_rb)
    RadioButton notPassRb;
    @BindView(R.id.audit_des_edit)
    EditText auditDesEdit;
    private ProgressDialog dialog;
    private long pro_work_trend_id;
    private String projectName;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {

        projectName = getIntent().getStringExtra(Constance.NAME);
        pro_work_trend_id = getIntent().getLongExtra(PRO_WORK_TREND_ID, -1);

        titleBar.setTitle("动态审核");

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialog == null) {
                    dialog = ProgressDialog.show(getActivity(), "提示", "正在提交");
                } else {
                    dialog.show();
                }
                HashMap<String, String> hashMap = new HashMap<>();

                hashMap.put("pro_work_trend_id", String.valueOf(pro_work_trend_id));

                int status = 3;
                String statusText = "不同意";
                if (passRb.isChecked()) {
                    status = 2;
                    statusText = "同意";
                }

                hashMap.put("auditStatus", String.valueOf(status));
                hashMap.put("auditStatusText", statusText);

                String remark = auditDesEdit.getText().toString().trim();
                hashMap.put("auditRemark", remark);


                ApiFactory.getInstance()
                        .submitProjectWorkTrendApproval(hashMap)
                        .compose(RxSchedulers.<Data>io_main())
                        .subscribe(new Consumer<Data>() {
                            @Override
                            public void accept(Data data) throws Exception {
                                dialog.dismiss();
                                setResult(200);
                                finish();
                                t("提交成功");
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                dialog.dismiss();
                                t("提交失败");
                            }
                        });

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_autid;
    }


    public String getContent() {
        return auditDesEdit.getText().toString().trim();
    }

    /**
     * @param context
     */
    public static void start(Activity context, long pro_work_trend_id, int requestCode, String projectName) {
        Intent intent = new Intent(context, ProjectTrendAutidActivity.class);
        intent.putExtra(PRO_WORK_TREND_ID, pro_work_trend_id);
        intent.putExtra(Constance.NAME, projectName);
        context.startActivityForResult(intent, requestCode);
    }

}
